package com.sdf.com.emjdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Created by sanpi on 2016/7/3.
 */

public class DefaultEmoGridView extends GridView implements AdapterView.OnItemClickListener {

    public void setOnEmoItemClickListener(OnEmoItemClickListener onEmoItemClickListener) {
        this.onEmoItemClickListener = onEmoItemClickListener;
    }

    private OnEmoItemClickListener onEmoItemClickListener;

    public interface OnEmoItemClickListener {
        void onEmoItemClick(int resId, String resName);//resName   smiley_00

        void onDelete();
    }

    private int firstEmoResId;

    public DefaultEmoGridView(Context context) {
        this(context, null);
    }

    public DefaultEmoGridView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DefaultEmoGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化
        //设置列数
        setNumColumns(7);
        //竖直方向上的距离
        float density = getResources().getDisplayMetrics().density;
        setVerticalSpacing((int) (10 * density));
        //设置适配器
        setAdapter(new MyAdapter());
        //设置点击事件
        setOnItemClickListener(this);
    }

    private int totalPage;
    private int curentPage;

    public void initData(int totalPage, int currentPage) {
        this.totalPage = totalPage;
        this.curentPage = currentPage;
        //计算当前页面第一个表情的资源id
        firstEmoResId = R.mipmap.smiley_00 + currentPage * 20;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (onEmoItemClickListener != null) {
            if (position == 20) {
                //删除键
                onEmoItemClickListener.onDelete();
            } else {
                int resId = firstEmoResId + position;//资源id
                int index = curentPage * 20 + position;//位置00 ->100
                String resName = null;
                if (index < 10) {
                    resName = "smiley_0" + index;
                } else {
                    resName = "smiley_" + index;
                }
                onEmoItemClickListener.onEmoItemClick(resId, resName);
            }
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 21;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(parent.getContext());
            //计算当前页面表情资源的id
            int id = firstEmoResId + position;
            if (position < 20) {
                imageView.setImageResource(id);
            } else {
                imageView.setImageResource(R.mipmap.del_btn_nor);
            }
            return imageView;
        }
    }
}
